<?php
/**
 * Created by PhpStorm.
 * User: wuzhentao
 * Date: 14-12-31
 * Time: 下午1:51
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 压缩图片
 * @param $local_path
 * @param $width
 * @param $height
 * @param string $new_image
 * @return mixed
 */
function image_resize($local_path, $width, $height = 0, $new_image = '') {
    $CI = & get_instance();
    $CI->load->library('image_lib');
    $size = @getimagesize($local_path);
    $create_size = calculate_image_size($size, $width);
    if($create_size[1] > $height) {
        $master_dim = 'width';
    } else {
        $master_dim = 'height';
        $create_size = calculate_image_size($size, $height, false);
    }
    if(!$height) {
        $master_dim = 'width';
    }
    $config['image_library'] = 'gd2';
    $config['source_image'] = $local_path;
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width'] = $create_size[0];
    if($height) {
        $config['height'] = $create_size[1];
    } else {
        $config['height'] = $create_size[0];
    }
    $config['master_dim'] =$master_dim;
    $config['thumb_marker'] = '';
    $config['new_image'] = $new_image;
    $config['dynamic_output'] = empty($new_image);
//    $config['dynamic_output'] = true;
    $CI->image_lib->initialize($config);
    return $CI->image_lib->resize();
}

/**
 * 图片智能裁剪
 * @param $local_path
 * @param $width
 * @param $height
 * @param string $new_image
 * @return bool|string
 */
function image_crop($local_path, $width, $height = 0, $new_image = '') {
    $CI = & get_instance();
    $CI->load->library('image_lib');
    $size = @getimagesize($local_path);
    $x_axis = ($size[0] - $width)/2;
    $y_axis = ($size[1] - $height)/2;
    $config['image_library'] = 'gd2';
    $config['source_image'] = $local_path;
    $config['x_axis'] = $x_axis;
    $config['y_axis'] = $y_axis;
    $config['width'] = $width;
    $config['height'] = $height;
    $config['new_image'] = $new_image;
    $config['maintain_ratio'] = FALSE;
    $config['dynamic_output'] = empty($new_image);
    $config['thumb_marker'] = '';
    $CI->image_lib->initialize($config);
    return $CI->image_lib->crop();
}

/**
 * 上传文件
 * @param $image
 * @param string $up_filename
 * @param string $folder
 * @return mixed
 */
function upload_image($image, $up_filename = '', $folder = '') {
    $CI = & get_instance();
    $CI->config->load('server');
    $CI->load->library('ftp');
    $ftp_config = $CI->config->item('ftp');
    $config['hostname'] = $ftp_config['host'];
    $config['username'] = $ftp_config['username'];
    $config['password'] = $ftp_config['password'];
    $config['debug'] = TRUE;
    $CI->ftp->connect($config);
    $list_files = $CI->ftp->list_files($ftp_config['attch_basepath']);
    if(empty($folder)) {
        $folder = $ftp_config['attch_basepath'] . date('Y_m', time());
    }
    if(array_search($folder, $list_files) === FALSE) {
        $CI->ftp->mkdir($folder, DIR_READ_MODE);
    }
    if(empty($up_filename)) {
        $info = explode('.', basename($image));
        $up_filename = md5($folder.basename($image).microtime()) . '.' . $info[1];
    }
    $upfile = $folder . '/' .$up_filename;
    $CI->ftp->upload($image, $upfile);
    $CI->ftp->close();
    $base_path = explode('/', $ftp_config['attch_basepath']);
    return str_replace($base_path[0] . '/', '', $upfile);
}

/**
 * 返回缩略图地址
 * @param $url
 * @param $width
 * @param $height
 * @return string
 */
function build_resize_image_url($url, $width, $height = 0) {
    $url = substr($url, strpos($url, '/') + 1);
    $basename = basename($url);
    $path = str_replace($basename, '', $url);
    $fileinfo = explode('.', $basename);
    $filename = $fileinfo[0] . '_' . $width . ($height ? '_' . $height : '');
    return THUMB_URL . $path . $filename . '.' . $fileinfo[1];
}

/**
 * 计算缩略尺寸
 * @param $size
 * @param $length
 * @param bool $iswidth
 * @return array
 */
function calculate_image_size($size, $length, $iswidth = true) {
    if($iswidth) {
        $ratio = $size[0] / $length;
        $height = $size[1] / $ratio;
        return array($length, $height);
    } else {
        $ratio = $size[1] / $length;
        $width = $size[0] / $ratio;
        return array($width, $length);
    }
}